<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="85px"
    >
      <el-form-item label="排班计划" prop="workPlanName">
        <el-input
          v-model="queryParams.workPlanName"
          placeholder="请输入排班计划名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查点名称" prop="checkName">
        <el-input
          v-model="queryParams.checkName"
          placeholder="请输入检查点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机姓名" prop="personName">
        <el-input
          v-model="queryParams.personName"
          placeholder="请输入司机姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker
          v-model="queryParams.daterange"
          type="daterange"
          align="right"
          size="small"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['report:checkPoint:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['report:checkPoint:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="checkPointList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column type="index" label="序号" width="60" align="left" />
      <el-table-column label="车牌" align="left" prop="carNum" />

      <el-table-column label="司机" align="left" prop="personName" />
      <el-table-column label="所属部门" align="left" prop="deptName" />
      <el-table-column label="排班计划" align="left" prop="workPlanName" />
      <el-table-column label="日期" align="left" prop="effectDate" />
      <el-table-column label="检查点名称" align="left" prop="checkName" />
      <el-table-column label="进入次数" align="left" prop="enterCount" />
      <el-table-column label="进入时间" align="left" prop="enterTime" />
      <!-- <el-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['report:checkPoint:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listCheckPoint,
  getCheckPoint,
  delCheckPoint,
  addCheckPoint,
  updateCheckPoint,
} from "@/api/report/checkPoint";

import dayjs from "dayjs";

export default {
  name: "CheckPoint",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 司机,车辆检查点工作及里程统计表格数据
      checkPointList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        targetType: "car",
        carNum: undefined,
        personName: undefined,
        workPlanName: undefined,
        effectDate: undefined,
        workPlanType: "car",
        daterange: undefined,
      },
    };
  },
  computed: {
    deptId() {
      return this.$store.getters.choosedDeptId;
    },
  },
  watch: {
    deptId: function () {
      this.queryParams.deptId = this.deptId;
      this.getList();
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询司机,车辆检查点工作及里程统计列表 */
    getList() {
      this.loading = true;
      this.queryParams.deptId = this.deptId;

      if (
        this.queryParams.daterange &&
        this.queryParams.daterange.length == 2
      ) {
        this.queryParams.effectStart = dayjs(
          this.queryParams.daterange[0]
        ).format("YYYY-MM-DD");
        this.queryParams.effectEnd = dayjs(
          this.queryParams.daterange[1]
        ).format("YYYY-MM-DD");
      } else {
        this.queryParams.effectStart = null;
        this.queryParams.effectEnd = null;
      }
      listCheckPoint(this.queryParams).then((response) => {
        this.checkPointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.daterange = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.reportPointId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const reportPointIds = row.reportPointId || this.ids;
      this.$modal
        .confirm('是否确认删除"' + (row.orderName || "已勾选") + '"的数据项?')
        .then(() => {
          this.loading = true;
          return delCheckPoint(reportPointIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "report/checkPoint/export",
        {
          ...this.queryParams,
        },
        `车辆检查点工作统计_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
